package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.IdGroupMapper;
import com.itheima.mapper.SetmealAndCheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.IdGroup;
import com.itheima.pojo.SetmealAndCheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private IdGroupMapper idGroupMapper;

    @Autowired
    private SetmealAndCheckGroupMapper setmealAndCheckGroupMapper;

    @Override
    public PageResult findPage(QueryPageBean qb) {

        int begin = (qb.getCurrentPage() - 1) * qb.getPageSize();
        List<CheckGroup> list = checkGroupMapper.findByPage(begin, qb.getPageSize());
        Long count = checkGroupMapper.count();

        for (CheckGroup checkGroup : list) {
            List<IdGroup> idGroupList = idGroupMapper.selectGroupId(checkGroup.getId());
            List<CheckItem> checkItemList = new ArrayList<>();
            for (IdGroup idGroup : idGroupList) {
                System.out.println(idGroupList);
                CheckItem checkItem = checkGroupMapper.selectItemId(idGroup.getCheckitemId());
                checkItemList.add(checkItem);
            }
            checkGroup.setCheckItems(checkItemList);
        }
        return new PageResult(count, list);
    }

    @Override
    public Result add(Integer[] checkitemIds, CheckGroup checkGroup) {
        //先将检查组放入数据库得到检查组在数据库的id
        checkGroupMapper.insertGroup(checkGroup);
        //拿到数据库里的id
        CheckGroup checkGroup1 = checkGroupMapper.selectByCode(checkGroup.getCode());
        //将checkgroupId和checkitemId放入t_checkgroup_checkitem里
        for (Integer checkitemId : checkitemIds) {
            idGroupMapper.insertGroup(checkGroup1.getId(), checkitemId);
        }
        return new Result(true, "新增检查组成功");
    }

    @Override
    public Result findById(Integer id) {
        CheckGroup checkGroup = checkGroupMapper.selectById(id);
        if (checkGroup == null) {
            return new Result(false, "查询结果不存在");
        }
        return new Result(true, "操作成功", checkGroup);
    }

    @Override
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        List<IdGroup> idGroupList = idGroupMapper.selectGroupId(id);
        List checkItemIdList = new ArrayList<>();
        for (IdGroup idGroup : idGroupList) {
            CheckItem checkItem = checkGroupMapper.selectItemId(idGroup.getCheckitemId());
            checkItemIdList.add(checkItem.getId());
        }
        return new Result(true,"操作成功",checkItemIdList);
    }

    @Override
    public Result modify(Integer[] checkitemIds, CheckGroup checkGroup) {
        //删除原有id
        idGroupMapper.deleteByCheckId(checkGroup.getId());
        //添加新的id
        for (Integer checkitemId : checkitemIds) {
            idGroupMapper.insertGroup(checkGroup.getId(), checkitemId);
        }
        checkGroupMapper.updateById(checkGroup);
        return new Result(true,"修改成功");
    }

    @Override
    public Result findAll() {
        List<CheckGroup> list = checkGroupMapper.selectList(null);
        return new Result(true,"查询成功",list);
    }

    @Override
    public Result findGroup(Integer id) {
        LambdaQueryWrapper<SetmealAndCheckGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealAndCheckGroup::getSetmealId,id);
        List<SetmealAndCheckGroup> setmealAndCheckGroups = setmealAndCheckGroupMapper.selectList(wrapper);
        List ids=new ArrayList();
        for (SetmealAndCheckGroup setmealAndCheckGroup : setmealAndCheckGroups) {
            ids.add(setmealAndCheckGroup.getCheckgroupId());
        }
        return new Result(true,"操作成功",ids);
    }
}
